﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Linq.Expressions;
using System.Globalization;
using Wedo.Mvc.Utility;
using System.Web.Mvc;
using Wedo.Recognition.Service;
using Wedo.Recognition.Domain;
using Wedo.Service.Common;
using Wedo.Domain.Models; 
using Wedo.Recognition.Repository;

namespace Wedo.Recognition.Web.Controllers
{
    public abstract class EntityController<T, TSearch> : EntityKeyController<T, Guid, TSearch>
         where T : Entity where TSearch:SearchModel
    {
        protected IUnitWork _dbContext;
        public EntityController(IUnitWork dbContext)
        {
            _dbContext = dbContext;
        }
 
        protected abstract IBaseService<T> EntityService { get; }

        protected override void DeleteModel(Guid id)
        {
            EntityService.Remove(id);
        }

        protected override T FindById(Guid Id)
        {
            return EntityService.GetById(Id);
        }
        protected override void Insert(T model)
        {
            model.CreatedBy = CurrentUserName;
            model.Created = DateTime.Now;
            model.ModifiedBy = CurrentUserName;
            model.Modified = DateTime.Now;

            EntityService.Add(model);
        }

        protected override void UpdateModel(T model)
        {
            model.ModifiedBy = CurrentUserName;
            model.Modified = DateTime.Now;

            EntityService.Update(model);
        }


        public ActionResult RediretToError(string msg)
        {
            return RedirectToAction("Error", "Home", new { msg=msg });
        }
    }
}